//
//  MHHomeCircleItemCollectionViewCell.m
//  MiaoCircle
//
//  Created by 许正荣 on 2018/4/13.
//  Copyright © 2018年 acgera. All rights reserved.
//

#import "MHHomeDiscoverItemCollectionViewCell.h"
#import "MHMomentVideo.h"
#import "NSMutableAttributedString+MHMoment.h"
#import "UIButton+ImageTitleSpacing.h"
#import "MHCustomItemViewModel.h"
#import "MHNearByModel.h"
#import "UIImageView+CornerRadius.h"
#import "MHUIKit.h"
@interface MHHomeDiscoverItemCollectionViewCell()

@property (nonatomic, readwrite, strong) MHHomeDiscoverItemViewModel *viewModel;
@property (weak, nonatomic) IBOutlet UIImageView *circleBgImagView;
@property (weak, nonatomic) IBOutlet UILabel *videoDurationTimeLabel;
//@property (weak, nonatomic) IBOutlet UILabel *circleNameLabel;
//@property (weak, nonatomic) IBOutlet UIButton *circleOwnerBtn;
@property(nonatomic, strong) YYLabel *nameLabel;
@property (weak, nonatomic) IBOutlet UIImageView *playImageView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *heightConstraint;

@property(nonatomic, assign) CGFloat height;

@end

@implementation MHHomeDiscoverItemCollectionViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    self.circleBgImagView.contentMode = UIViewContentModeScaleAspectFill;
    self.circleBgImagView.masksToBounds = YES;
    
//    [self.circleBgImagView.layer setMasksToBounds:YES];
    //    self.circleOwnerBtn.imageView.layer.masksToBounds = YES;
    //    self.layer.cornerRadius = 6.0;
    //    self.layer.masksToBounds = YES;x
//    self.masksToBounds
    
//    self.backgroundColor = [UIColor whiteColor];
//    UIBezierPath * path = [UIBezierPath bezierPathWithRoundedRect:self.bounds cornerRadius:6];
//
//    CAShapeLayer * layer = [[CAShapeLayer alloc]init];
//
//    layer.frame = self.bounds;
//
//    layer.path = path.CGPath;
//
//    self.layer.mask = layer;
    
    
    self.bgImage.image = [UIImage imageNamed:@"bg_found_discover"];
    self.contentView.backgroundColor = [UIColor clearColor];
//    [self.circleBgImagView zy_cornerRadiusAdvance:6 rectCornerType:UIRectCornerTopLeft | UIRectCornerTopRight];
    self.circleBgImagView.layer.cornerRadius = 6;
    self.circleBgImagView.masksToBounds = YES;
    self.ownerImageView.contentMode = UIViewContentModeScaleAspectFit;
    
    self.ownerLabel.font = MHMediumFont(10);
    self.broserBtn.titleLabel.font = MHRegularFont_10;
    self.attriAcountBtn.titleLabel.font = MHRegularFont_10;
    self.videoDurationTimeLabel.font = MHMediumFont(12);
    self.discovermaskView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"icon_discover_mask"]];
    [self.contentView addSubview:self.nameLabel];
    self.positionBtn.titleLabel.font = MHRegularFont_10;
    [self.positionBtn layoutButtonWithEdgeInsetsStyle:MKButtonEdgeInsetsStyleLeft imageTitleSpace:5];
    self.ownerImageView.layer.cornerRadius = 10;
    self.ownerImageView.layer.masksToBounds = YES;
//    [self.ownerImageView zy_cornerRadiusAdvance:10 rectCornerType:UIRectCornerAllCorners];
    
}

-(YYLabel *)nameLabel
{
    if (!_nameLabel) {
       _nameLabel =  [MHUIKit cofigYYLableWithTextColor:MHMainTextColor font:MHRegularFont_13 numberOfLines:0 backgroundColor:nil];
    }
    return _nameLabel;
}
-(void)layoutSubviews
{
    [super layoutSubviews];
    
    
    
    if ([self.viewModel isKindOfClass:[MHHomeDiscoverItemViewModel class]]) {
        [self hiddenPlayView:self.viewModel.homeDiscover];
        [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.circleBgImagView.mas_bottom).offset(10);
            make.left.equalTo(self).offset(10);
//            make.right.equalTo(self).offset(-10);
//            make.width.mas_lessThanOrEqualTo((MH_SCREEN_WIDTH - 20) / 2 - 20 );
//            make.bottom.equalTo(self.ownerImageView.mas_top).offset(-5);
        }];
    }else
    {
        MHCustomItemViewModel *model =  (MHCustomItemViewModel *)self.viewModel;
        [self hiddenPlayView:model.model];
    }
    
}

-(void) hiddenPlayView:(MHDiscoverHomeModel *) model
{
    if (model.attm)
    {
        self.playImageView.hidden = YES;
    }else
    {
        self.playImageView.hidden = NO;
    }
}

- (void)bindViewModel:(MHHomeDiscoverItemViewModel *)viewModel{
    self.viewModel = viewModel;
    if (![viewModel isKindOfClass:[MHHomeDiscoverItemViewModel class]]) {
        MHCustomItemViewModel *model = viewModel;
        viewModel = [[MHHomeDiscoverItemViewModel alloc] init];
        viewModel.homeDiscover = model.model;
        viewModel.heightConstraint = 230.0f;
        
        self.reaPacketBtn.hidden = (viewModel.homeDiscover.redInfo == nil);
        [self.reaPacketBtn configRedPacketWithRedPacketWithRedInfo:viewModel.homeDiscover.redInfo redPacketPositon:RedPackePostionNearby];
        
        
        [self.positionBtn setTitle:[NSString stringWithFormat:@" %@",viewModel.homeDiscover.location.areaName] forState:0];
        CGFloat imageWith = (MH_SCREEN_WIDTH - 20) / 2;
        
        //       CGFloat tempScale = imageWith / 230.0;
        YYTextLayout *nameLableLayout;
        if(MHStringIsNotEmpty(viewModel.homeDiscover.content))
        {
            NSMutableAttributedString *textAttr = [[NSMutableAttributedString alloc] initWithString:viewModel.homeDiscover.content];
            textAttr.yy_font = MHMediumFont(14);
            textAttr.yy_color = MHMainTextColor;
            textAttr.yy_lineBreakMode = NSLineBreakByCharWrapping;
            textAttr.yy_alignment = NSTextAlignmentLeft;
            NSMutableAttributedString *askedAttr;
            BOOL isAsk = MHStringIsNotEmpty(viewModel.homeDiscover.askedUName);
            NSString *askString = [NSString stringWithFormat:@"向%@提问\n", viewModel.homeDiscover.askedUName];
            if (isAsk) {
                askedAttr = [[NSMutableAttributedString alloc] initWithString:askString];
                askedAttr.yy_color = MHAlertTextColor;
                askedAttr.yy_font = MHRegularFont_14;
                askedAttr.yy_alignment = NSTextAlignmentLeft;
                [textAttr insertAttributedString:askedAttr atIndex:0];
            }
            [textAttr mh_regexContentWithWithEmojiImageFontSize:14];
            YYTextContainer *nameLableContainer = [YYTextContainer containerWithSize:CGSizeMake(imageWith -20, MAXFLOAT)];
            nameLableContainer.maximumNumberOfRows = 1;
            nameLableLayout = [YYTextLayout layoutWithContainer:nameLableContainer text:textAttr.copy];
            viewModel.tileTextLayout = nameLableLayout;
        }
        _nameLabel.textLayout = viewModel.tileTextLayout;
        _nameLabel.frame = CGRectMake(10, viewModel.heightConstraint + 10 , viewModel.tileTextLayout.textBoundingSize.width, viewModel.tileTextLayout.textBoundingSize.height);
        
        viewModel.homeDiscover.distance = viewModel.homeDiscover.distance > 0.1 ? viewModel.homeDiscover.distance : 0.1;
        self.videoDurationTimeLabel.text = [NSString stringWithFormat:@"%.1fkm",viewModel.homeDiscover.distance];
        self.ownerLabel.text = viewModel.homeDiscover.publishUserName;
    }else
    {
        self.reaPacketBtn.hidden = (viewModel.homeDiscover.redInfo == nil);
        [self.reaPacketBtn configRedPacketWithRedPacketWithRedInfo:viewModel.homeDiscover.redInfo redPacketPositon:RedPackePostionNearby];
        
        if(MHStringIsNotEmpty(viewModel.homeDiscover.content))
        {
            NSMutableAttributedString *textAttr = [[NSMutableAttributedString alloc] initWithString:viewModel.homeDiscover.content];
            textAttr.yy_font = MHMediumFont(13);
            textAttr.yy_color = MHMainTextColor;
            textAttr.yy_lineBreakMode = NSLineBreakByCharWrapping;
            textAttr.yy_alignment = NSTextAlignmentLeft;
            NSMutableAttributedString *askedAttr;
            BOOL isAsk = MHStringIsNotEmpty(viewModel.homeDiscover.askedUName);
            NSString *askString = [NSString stringWithFormat:@"向%@提问\n", viewModel.homeDiscover.askedUName];
            if (isAsk) {
                askedAttr = [[NSMutableAttributedString alloc] initWithString:askString];
                askedAttr.yy_color = MHAlertTextColor;
                askedAttr.yy_font = MHRegularFont_13;
                askedAttr.yy_alignment = NSTextAlignmentLeft;
                [textAttr insertAttributedString:askedAttr atIndex:0];
            }
            [textAttr mh_regexContentWithWithEmojiImageFontSize:14];
            self.nameLabel.numberOfLines = 2;
            self.nameLabel.preferredMaxLayoutWidth = (MH_SCREEN_WIDTH - 20) / 2 - 20;
            self.nameLabel.attributedText =  textAttr;
            
            
        }
      
//        self.viewModel.cellSize = CGSizeMake(self.viewModel.cellSize.width, viewModel.heightConstraint + 50 + self.nameLabel.height + 5);
//        _nameLabel.textLayout = viewModel.tileTextLayout;
//        _nameLabel.frame = CGRectMake(10, viewModel.heightConstraint + 10 , viewModel.tileTextLayout.textBoundingSize.width, viewModel.tileTextLayout.textBoundingSize.height);
            self.videoDurationTimeLabel.text = viewModel.homeDiscover.circleName;
//         [self.ownerLabel setTitle:viewModel.homeDiscover.publishUserName forState:0];
        self.ownerLabel.text = viewModel.homeDiscover.publishUserName;
    }
   
    self.heightConstraint.constant = viewModel.heightConstraint;
    
    NSURL *imageUrl;
    if (viewModel.homeDiscover.attm) {
        imageUrl = [NSURL URLWithString:viewModel.homeDiscover.attm.fullPath];
//        [self.circleBgImagView yy_setImageWithURL:[NSURL URLWithString:viewModel.homeDiscover.attm.fullPath ] placeholder:MHWebImagePlaceholder() options:YYWebImageOptionIgnoreImageDecoding completion:nil];
    }else
    {
        imageUrl = viewModel.homeDiscover.vedioObj.coverURL;
//        [self.circleBgImagView yy_setImageWithURL:viewModel.homeDiscover.vedioObj.coverURL  placeholder:MHWebImagePlaceholder() options:YYWebImageOptionIgnoreImageDecoding completion:nil];
        
//         placeholderImage:<#(nullable UIImage *)#>
        
//        SDWebImageProgressiveDownload
//        [self.circleBgImagView sd_setImageWithURL:viewModel.homeDiscover.vedioObj.coverURL  placeholderImage:MHWebImagePlaceholder() options:SDWebImageProgressiveDownload];
        
        
//        [self.circleBgImagView yy_setImageWithURL:viewModel.homeDiscover.vedioObj.coverURL placeholder:MHWebImagePlaceholder() options:nil completion:^(UIImage * _Nullable image, NSURL * _Nonnull url, YYWebImageFromType from, YYWebImageStage stage, NSError * _Nullable error) {
//            dispatch_async(dispatch_get_global_queue(0, 0), ^{
//                UIImage *copressImage = [UIImage imageWithData:[image compressWithLengthLimit:30 *1024]] ;
//                dispatch_async(dispatch_get_main_queue(), ^{
//                    self.circleBgImagView.image = copressImage;
//                });
//            });
        
//            UIImage *corpimage = [image yy_imageByResizeToSize:CGSizeMake(self.circleBgImagView.width, self.circleBgImagView.height)];
//            self.circleBgImagView.image = corpimage;
//        }];
//        [self.circleBgImagView yy_setImageWithURL:[NSURL URLWithString:viewModel.homeDiscover.allPublishUserHeadPic] forState:0 placeholder:MHDefaultAvatar(MHDefaultAvatarTypeDefualt) options:nil completion:^(UIImage * _Nullable image, NSURL * _Nonnull url, YYWebImageFromType from, YYWebImageStage stage, NSError * _Nullable error) {
//
//                }];
    }
//   [self.circleBgImagView sd_setImageWithURL:imageUrl  placeholderImage:MHWebImagePlaceholder()];
    [self.circleBgImagView yy_setImageWithURL:imageUrl  placeholder:MHWebImagePlaceholder()];
//    [self.circleBgImagView yy_setImageWithURL:imageUrl  placeholder:MHWebImagePlaceholder() options:YYWebImageOptionAllowBackgroundTask completion:nil];
    [self.attriAcountBtn setTitle:[NSString stringWithFormat:@" %d",viewModel.homeDiscover.viewCount] forState:0];
    [self.broserBtn setTitle:[NSString stringWithFormat:@" %d",viewModel.homeDiscover.likeCount] forState:0];
    
    
    [self.ownerImageView yy_setImageWithURL:[NSURL URLWithString:viewModel.homeDiscover.allPublishUserHeadPic] placeholder:MHDefaultAvatar(MHDefaultAvatarTypeDefualt)];
    
    [self.circleBgImagView setNeedsLayout];
    [self.circleBgImagView layoutIfNeeded];
   
//    self.circleOwnerBtn.imageView zy_cornerRadiusAdvance: rectCornerType:<#(UIRectCorner)#>
//    [self.circleOwnerBtn yy_setImageWithURL:[NSURL URLWithString:viewModel.homeDiscover.allPublishUserHeadPic] forState:0 placeholder:MHDefaultAvatar(MHDefaultAvatarTypeDefualt) options:nil completion:^(UIImage * _Nullable image, NSURL * _Nonnull url, YYWebImageFromType from, YYWebImageStage stage, NSError * _Nullable error) {
//
//        [self.circleOwnerBtn setImage:[image yy_imageByCropToRect:] forState:0];
////        [self.circleOwnerBtn setNeedsLayout];
////        [self.contentView layoutIfNeeded];
//    }];
    
}


@end
